<!-- 
	回执录入页面
 -->
<template>
	<div>
		<div id="container">
			<div class="row">
				<div class="col-3 border" style="">
						<div>
							<label class="form-control">巡检线路:</label>
							
							<el-dropdown size="medium" split-button type="primary"  @command="handleCommand">
							    <span v-text="this.addInspectiontaskDetailObj.linename"></span>
							    <el-dropdown-menu slot="dropdown">
							        <el-dropdown-item v-for="item in psPoleInfoByid" :command="{polecode:item.polecode,poleid:item.id}" :key="item.id" v-text="item.linename"></el-dropdown-item>
							    </el-dropdown-menu>
							</el-dropdown>
						</div>	
						
				</div>
				<div class="col-2 border" style="display: flex; justify-content: center; align-items: center;text-align: center;color:black;">
						<div class=h3>
							==>>>
						</div>
				</div>
				<div v-if="isFlag==false" class="col-7 border" style="">
					<div >
						<div class="row">
							<div class="col-md-6">
								<label class="form-label" >线路编号:</label>
								<span v-text="addInspectiontaskDetailObjs.linecode"></span>
							</div>
						</div>
						<div class="row">
							<div class="col-md-6">
								<label class="form-label">杆塔编号:</label>
								<span  v-text="addInspectiontaskDetailObjs.polecode"></span>
							</div>
						</div>
						<div class="row">
							<div class="col-md-6">
								<label class="form-label" >缺陷类型:</label>
								<select class="" @change="chagneGetType()" v-model="selectedTypeId">
										  <option value="-1" selected>--请选择--</option>
										  <option  v-for="item in queryByIsType"  :value="item.configvalueid" :key="item.id">{{ item.configvaluename }}</option>
										</select>
							</div>
						</div>
						
						<div class="row">
							<div class="col-md-6">
								<label class="form-label">缺陷级别:</label>
								<select class="" @change="chagneGetLevel()" v-model="selectedLeveId">
										  <option value="-1" selected>--请选择--</option>
										  <option v-for="item in queryByIsLevel" :value="item.configvalueid"  :key="item.id"  >{{ item.configvaluename }}</option>
										</select>
							</div>
						</div>
						<div class="row">
							<div class="col-md-6">
								<label class="form-label" >完好率:</label>
								<input type="text" name="" v-model="addInspectiontaskDetailObjs.intactrate" />
							</div>
						</div>
						<div class="row">
							<div class="col">
								<label class="form-label" >发现时间:</label>
								<span v-text="addInspectiontaskDetailObjs.discovertime"></span>
								
							</div>
						</div>
						<div class="row">
							<div class="col-md-6">
								<label class="form-label" >发现人员:</label>
								<span  v-text="addInspectiontaskDetailObjs.discoverername"></span>
								
							</div>
						</div>
						<div class="row">
							<div class="col-md-6">
								<label class="form-label" >缺陷描述:</label> 
								<textarea rows="3"  v-model="addInspectiontaskDetailObjs.bugdesc"  cols="20"></textarea>
							</div>
						</div>
					
					</div>
				</div>
				
				<div v-if="isFlag==true" class="col-7 border" style="">
					<div >
						<div class="row">
							<div class="col-md-6">
								<label class="form-label" >线路编号:</label>
								<span v-text="addInspectiontaskDetailObj.linecode"></span>
							</div>
						</div>
						<div class="row">
							<div class="col-md-6">
								<label class="form-label">杆塔编号:</label>
								<span  v-text="addInspectiontaskDetailObj.polecode"></span>
							</div>
						</div>
						<div class="row">
							<div class="col-md-6">
								<label class="form-label" >缺陷类型:</label>
								<select class="" @change="chagneGetType()" v-model="selectedTypeId">
										  <option value="-1" selected>--请选择--</option>
										  <option  v-for="item in queryByIsType" :value="item.configvalueid"  :key="item.id" >{{ item.configvaluename }}</option>
										</select>
								
							</div>
						</div>
						
						<div class="row">
							<div class="col-md-6">
								<label class="form-label">缺陷级别:</label>
								<select class="" @change="chagneGetLevel()" v-model="selectedLeveId">
										  <option value="-1" selected>--请选择--</option>
										  <option v-for="item in queryByIsLevel" :value="item.configvalueid"  :key="item.id"  >{{ item.configvaluename }}</option>
										</select>
							</div>
						</div>
						<div class="row">
							<div class="col-md-6">
								<label class="form-label" >完好率:</label>
								<input type="text" name="" v-model="addInspectiontaskDetailObj.intactrate" />
							</div>
						</div>
						<div class="row">
							<div class="col">
								<label class="form-label" >发现时间:</label>
								<span v-text="addInspectiontaskDetailObj.discovertime"></span>
								
							</div>
						</div>
						<div class="row">
							<div class="col-md-6">
								<label class="form-label" >发现人员:</label>
								<span  v-text="addInspectiontaskDetailObj.discoverername"></span>
								
							</div>
						</div>
						<div class="row">
							<div class="col-md-6">
								<label class="form-label" >缺陷描述:</label> 
								<textarea rows="3"  v-model="addInspectiontaskDetailObj.bugdesc"  cols="20"></textarea>
								
							</div>
						</div>
					
					</div>
				</div>
			</div>
			<button type="button" class="btn btn-outline-primary" v-if="allCurrentFlag" @click="uploadReceipts">上传回执</button>
			<button type="button" class="btn btn-outline-primary" @click="save()">保存</button>
			<button type="button" class="btn btn-outline-primary" @click="$router.go(-1)">返回</button>
		</div>
		
	</div>
</template>

<script>
	import {
		queryByPsInspectiontaskMain,
		queryListById,
		psPoleInfoById,
		queryByIsLevel,
		queryByIsType,
		addPsInspectiontaskDetail,
		queryPstaDeObj,
		updatePstaDeObj,
		queryPsTaDeById,
		updatePsInspectiontaskMain
	} from '../../config/request.js'
	export default {
		data() {
			return {
				user:{
					// 登录的用户 
					usercode:'1',
					username:'admin'
				},
				psInspectorDetail:{
					
				},
				psPoleInfoByid:{
					// 所属线路 的 所有 杆塔
				}
				,
				selectedTypeId:"-1", // 选择的下拉框的id
				selectedLeveId:"-1", // 选择的下拉框的id
				queryByIsLevel:{},//缺陷级别
				queryByIsType:{},// 缺陷类型
				addInspectiontaskDetailObj:{},// 添加 巡检任务子表
				addInspectiontaskDetailObjs:{}, // 查询巡检任务子表  根据 杆塔和线路名称查询 
				updatepsInspectiontaskMainObj:{},//修改 主表 任务状态 已完成
				queryPstaDeObj:{} ,// 查询 参数 
				isFlag:false, //为true 添加 ，false 的时候修改
				allCurrentFlag:false,//判断当前 巡检 线路 是否 全部 录入 为true 时 表示全部录入
				handleCommands:false,// 判断是否选择下拉框
			}
		},
		methods: {
			load(){
				// 查询 线路 下面的 所有 杆塔
				psPoleInfoById(this.addInspectiontaskDetailObj.linecode).then(json=>{
					this.psPoleInfoByid=json.data.queryListById
					
					
					 // 在这里进行数据循环处理
					Promise.all(
					  this.psPoleInfoByid.map(item => {
					    return queryPsTaDeById(item.id).then(json => {
					      return json.data.queryPsTaDeById;
					    });
					  })
					).then(results => {
					  this.allCurrentFlag = results.every(item => item !== null);
					});

							
							
				})
				
				
				queryByIsLevel().then(json=>{
					this.queryByIsLevel=json.data.queryByIsLevel
				})
				queryByIsType().then(json=>{
					this.queryByIsType=json.data.queryByIsType
				})
				
				
			},
			save(){
				if(this.handleCommands){
					if(this.isFlag){
						// 保存 添加
						this.addInspectiontaskDetailObj.issolve=0 //是否消缺（1、是，0、否）默认为0
						this.addInspectiontaskDetailObj.isBugLevel=0 //是否确定缺陷级别（1、是，0、否）默认为0
						this.addInspectiontaskDetailObj.createdby=this.user.username
						console.log(this.addInspectiontaskDetailObj)
						addPsInspectiontaskDetail(this.addInspectiontaskDetailObj).then(json=>{
							alert(json.message)
						})
					}else{
						// 修改
						console.log(this.addInspectiontaskDetailObjs)
						updatePstaDeObj(this.addInspectiontaskDetailObjs).then(json=>{
							alert(json.message)
						})
					}
				}else{
					alert("请选择杆塔！！！")
				}
				
				
				
			},
			uploadReceipts(){
				this.getCurrentDate()
				//上传回执
				let flag = confirm("确定上传吗?")
				if(flag){
					console.log(this.updatepsInspectiontaskMainObj)
					updatePsInspectiontaskMain(this.updatepsInspectiontaskMainObj).then(json=>{
						alert(json.message)
					})
				}
			},
			chagneGetType(){
				if(this.selectedTypeId!='-1'){
					 const selectedLine = this.queryByIsType.find(item => item.configvalueid === this.selectedTypeId);
				      console.log(selectedLine); // 输出选中的线路对象
					  this.addInspectiontaskDetailObj.bugtype=selectedLine.configvalueid // 缺陷 类型ID
					  this.addInspectiontaskDetailObj.bugtypename=selectedLine.configvaluename // 缺陷 类型NAME
					  
					  this.addInspectiontaskDetailObjs.bugtype=selectedLine.configvalueid // 缺陷 类型ID
					  this.addInspectiontaskDetailObjs.bugtypename=selectedLine.configvaluename // 缺陷 类型NAME
				}
			},
			chagneGetLevel(){
				if(this.selectedLeveId!='-1'){
					 const selectedLine = this.queryByIsLevel.find(item => item.configvalueid === this.selectedLeveId);
				      console.log(selectedLine); // 输出选中的线路对象
					  this.addInspectiontaskDetailObj.buglevel=selectedLine.configvalueid // 缺陷 级别ID
					  this.addInspectiontaskDetailObj.buglevelname=selectedLine.configvaluename // 缺陷 级别NAME
					
					// 修改 对象
					this.addInspectiontaskDetailObjs.buglevel=selectedLine.configvalueid // 缺陷 级别ID
					this.addInspectiontaskDetailObjs.buglevelname=selectedLine.configvaluename // 缺陷 级别NAME
				}
			},
			// 监听 下拉框的 选项
			 handleCommand({polecode, poleid}) {
				 this.handleCommands=true;
			        // 在这里处理 polecode 和 poleid
			        console.log('选中的id:', polecode);
			        console.log('选中的poleid:', poleid);
					this.addInspectiontaskDetailObj.poleid=poleid
					this.addInspectiontaskDetailObj.polecode=polecode // 杆塔CODE
					
					
					// 修改 对象
					this.addInspectiontaskDetailObj.discoverercode=this.user.usercode // 发现人员id 为当前登录巡检员
					this.addInspectiontaskDetailObj.discoverername=this.user.username  // 发现人员名称 为当前登录巡检员
					
					
					this.getInfo()
					
					this.getCurrentDate();
			},
			// 判断当前的 杆塔是否有数据 
			getInfo(){
				this.queryPstaDeObj.poleid=this.addInspectiontaskDetailObj.poleid
				this.queryPstaDeObj.linename=this.addInspectiontaskDetailObj.linename 
				queryPstaDeObj(this.queryPstaDeObj).then(json=>{
					if(json.data.queryByObj!=null){
						this.isFlag=false;
						this.addInspectiontaskDetailObjs=json.data.queryByObj
						alert("该杆塔已有录入信息！可进行修改！")
						
					}else{
						this.isFlag=true;
						alert("该杆塔暂无数据!请为该杆塔录入信息！")
					}
					
				})
			}
			,
			getCurrentDate() {
			     const date = new Date();
			     const year = date.getFullYear();
			     const month = ('0' + (date.getMonth() + 1)).slice(-2);
			     const day = ('0' + date.getDate()).slice(-2);
			     const hours = ('0' + date.getHours()).slice(-2);
			     const minutes = ('0' + date.getMinutes()).slice(-2);
			     const seconds = ('0' + date.getSeconds()).slice(-2);
				 //发现时间
			     this.addInspectiontaskDetailObj.discovertime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;/* ${hours}:${minutes}:${seconds} */
				 // 巡检时间
				 this.addInspectiontaskDetailObj.inspecttime=`${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; 
				 // 创建时间
				 this.addInspectiontaskDetailObj.creationtime=`${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; 
				 // 最后修改时间
				 this.addInspectiontaskDetailObj.lastUpdatetime=`${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; 
				 
				 
				 this.updatepsInspectiontaskMainObj.finishtime=`${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; 
			   },
		},
		mounted() {
			this.load()
			
			
		},
		created() {
			// 接收参数
		    this.addInspectiontaskDetailObj.linecode = this.$route.query.lineid; //线路编号
			this.addInspectiontaskDetailObj.linename = this.$route.query.linename; //线路名称
			this.addInspectiontaskDetailObj.taskid = this.$route.query.inspectiontaskcode; //主表任务编号
			this.addInspectiontaskDetailObj.taskstatus = this.$route.query.taskstatus; // 主表状态id
			this.addInspectiontaskDetailObj.isbug = 0; // 有无缺陷/故障（1、有，0、无）默认为0
			
			
			this.updatepsInspectiontaskMainObj.ps_Id = this.$route.query.ps_Id; //主表任务ID
			this.updatepsInspectiontaskMainObj.taskstatus=4;
			this.updatepsInspectiontaskMainObj.taskstatusname="已完成";
		  }
	}
</script>

<style lang="less" scoped>
	#container {
		background-color: #DCDFE6;
		border-radius: 5px;
		margin-top: 5%;
		padding: 20px;
		
	}
	#container div label {
	    font-size: 18px;
		margin-right: 10px; /* 例如，将右边距设置为 10 像素 */
	}
	
	#container div span {
	     font-family: 宋体;
	}
	
</style>